import datetime

from django.views.decorators.http import require_http_methods

from back.models import StatManual
from forum.models import Topic, Comment
from user_app.models import AccountInfo, AccountFollow
from utils.common_utils import json_response, Pagination
from utils.decorators import check_login
from django.db import connection


def get_most_activity_account(request):
    # 获取前10最活跃用户，发帖最多+回帖最多
    SQL = r"SELECT forum_comment.created_by_id, count(forum_comment.created_by_id) as total FROM forum_comment left join forum_topic on forum_comment.topic_id = forum_topic.id group by forum_comment.created_by_id order by total desc limit 5;"
    with connection.cursor() as cursor:
        cursor.execute(SQL)
        result_list = []
        for row in cursor.fetchall():
            account_id = row[0]
            total = row[1]
            ac = AccountInfo.objects.get(id=account_id)
            tmp = {
                "account_id": account_id,
                "nick_name": ac.nick_name,
                "avatar": ac.avatar,
                "total": total,
            }
            result_list.append(tmp)
        return json_response({"code": 0, "message": "", "data": result_list})


@require_http_methods(['GET'])
def get_stat_info(request):
    # 首页3个数据统计
    ac_count = AccountInfo.objects.count()
    topic_count = Topic.objects.count()
    cmt_count = Comment.objects.count()
    today = datetime.date.today()
    today_register = AccountInfo.objects.filter(create_time__gt=today).count()

    usercount_add = StatManual.objects.get(stat_name='注册总人数').add_value
    topiccount_add = StatManual.objects.get(stat_name='话题总数').add_value
    cmtcount_add = StatManual.objects.get(stat_name='评论总数').add_value
    todayregister_add = StatManual.objects.get(stat_name='今日注册').add_value
    result_dict = {
        "usercount": ac_count+usercount_add,
        "topiccount": topic_count+topiccount_add,
        "cmtcount": cmt_count+cmtcount_add,
        "todayregister": today_register+todayregister_add,
    }
    return json_response({"code": 0, "message": "", "data": result_dict})


@check_login
def get_account_list(request):
    page = int(request.GET.get('page', "1"))
    per_page = int(request.GET.get("per_page", "10"))

    ac_count = AccountInfo.objects.count()
    p = Pagination(page=page, per_page=per_page, total=ac_count)
    ac_list = AccountInfo.objects.all().order_by("-create_time")[p.offset:p.offset + per_page]
    result_list = []
    for item in ac_list:
        tmp = {
            "id": item.id,
            "nick_name": item.nick_name,
            "avatar": item.avatar,
            "slogan": item.slogan,
            "sex": item.sex,
            "create_time": item.create_time,
            "last_login": item.last_login,
            "mobile_phone": item.mobile_phone,
            "is_follow": AccountFollow.objects.filter(created_by_id=request.session['account_id'], follow_account_id=item.id).exists()
        }
        result_list.append(tmp)
    return json_response({"code": 0, "message": "", "data": {"item": result_list, "page_info": p.dict}})
